
#include "transfer/ReadWriteLock.h"

namespace ecarx::transfer::lock {

RWLock::RWLock() {
    pthread_rwlock_init(&_lock, NULL);
}

int RWLock::read() {
    return pthread_rwlock_rdlock(&_lock);
}
int RWLock::write() {
    return pthread_rwlock_wrlock(&_lock);
}
int RWLock::unlock() {
    return pthread_rwlock_unlock(&_lock);
}

AutoRead::AutoRead(RWLock* p) {
    _lock = p;
    _lock->read();
}

AutoRead::~AutoRead() {
    _lock->unlock();
}

AutoWrite::AutoWrite(RWLock* p) {
    _lock = p;
    _lock->write();
}

AutoWrite::~AutoWrite() {
    _lock->unlock();
}
}  // namespace swc::lock
